package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.bo.BlogCategoryBO;
import com.blog.entity.BlogCategory;
import com.blog.service.CategoryService;
import com.blog.utils.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Huangzp
 * @Date 2022/1/6 17:20
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private BlogCategoryBO blogCategoryBO;


    @Override
    public List<BlogCategory> findAll() {
        return blogCategoryBO.list();
    }

    @Override
    public void saveCategory(BlogCategory category) {

        if(category.getId()==null){
            int count = blogCategoryBO.count();
            if(count>5){
                throw new BusinessException("菜单栏最多五个");
            }
            blogCategoryBO.save(category);
        }else {
            blogCategoryBO.updateById(category);
        }
    }

    @Override
    public List<BlogCategory> getCategoryPage(BlogCategory category) {
        QueryWrapper<BlogCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(category.getName()), BlogCategory::getName, category.getName());

        return blogCategoryBO.list(queryWrapper);
    }

    @Override
    public void delCategory(List<Integer> ids) {
        blogCategoryBO.removeByIds(ids);
    }
}
